package com.fivestar.pmanager.service;

import static com.fivestar.pmanager.core.util.CodeHelper.isNotNullOrEmpty;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fivestar.pmanager.constants.CommonConstants;
import com.fivestar.pmanager.core.base.BaseCommonParameterBean;
import com.fivestar.pmanager.core.exception.DMLNoRecordEffectedException;
import com.fivestar.pmanager.core.service.BaseServiceImpl;
import com.fivestar.pmanager.dao.mapper.FsUserMapper;
import com.fivestar.pmanager.model.FsUserBean;

/**
 * 用户
 * FsUserService
 *
 * @author
 */
@Service("fsUserService")
public class FsUserService extends BaseServiceImpl<FsUserBean> implements CommonConstants 
{	
    private final static Logger log = Logger.getLogger(FsUserService.class);
    
    @Autowired
    private FsUserMapper mapper;

    @Override
    public int insert(FsUserBean mdUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException {
        mdUser.initBeforeCreate(commonParam.getActorId());
        int result;
        if ((result = mapper.insert(mdUser)) != 1) {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public int delete(FsUserBean fsUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException {
    	fsUser.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.delete(fsUser)) != 1) {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }

    @Override
    public int deletePhysics(FsUserBean mdUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException {
        int result;
        if ((result = mapper.deletePhysics(mdUser)) != 1) {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public int update(FsUserBean mdUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException {
        mdUser.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.update(mdUser)) != 1) {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public int updateBySelective(FsUserBean mdUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException {
        mdUser.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.updateBySelective(mdUser)) != 1) {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public List<FsUserBean> getList(FsUserBean mdUser) {
        return mapper.getList(mdUser);
    }


    @Override
    public FsUserBean getSingleById(Integer arg0) {
        return mapper.getSingleById(arg0);
    }


    @Override
    public int getCountOfPage(FsUserBean mdUser) {
        return mapper.getCountOfPage(mdUser);
    }


    @Override
    public List<FsUserBean> getListOfPage(FsUserBean mdUser) {
        return mapper.getListOfPage(mdUser);
    }


    @Override
    public int getCount(FsUserBean mdUser) {
        return mapper.getCount(mdUser);
    }


    public FsUserBean getValideByCardNo(String cardNo){
        return mapper.getValideByCardNo(cardNo);
    }

    @Override
    public void save(FsUserBean mdUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException {
        if (mdUser.getRowId() == null) {
            insert(mdUser, commonParam);
        } else {
            updateBySelective(mdUser, commonParam);
        }
    }


    @Override
    public FsUserBean getSingle(FsUserBean mdUser) {
        return mapper.getSingle(mdUser);
    }

    /**
     * 根据用户名或者手机号加载用户信息
     *
     * @param loginId
     * @return
     */
    public FsUserBean loadByNameOrPhone(String loginId) {
        return mapper.loadByNameOrPhone(loginId);
    }


    /**
     * 根据用户手机号查询
     *
     * @param phone
     * @return
     */
    public FsUserBean getByPhone(String phone) {
        return mapper.getByPhone(phone);
    }

    /**
     * 根据用户名查询
     *
     * @param name
     * @return
     */
    public FsUserBean getByName(String name) {
        return mapper.getByName(name);
    }

    public FsUserBean getByPhoneForService(String phone) {
        return mapper.getByPhoneForService(phone);
    }


    /**
     * 根据用户属性查询用户
     *
     * @param attributes 用户属性列表
     * @return List<FsUserBean>
     */
    public List<FsUserBean> findByAttributes(String [] attributes)
    {
        if(isNotNullOrEmpty(attributes)) return mapper.findByAttributes(attributes);
        return null;
    }

    /**
     * 查找记录返回listmap
     *
     * @param FsUserBean 查询条件
     * @return List<Map<String,Object>>
     */
    public List<Map<String,Object>> findForSearch(FsUserBean FsUserBean)
    {
        return mapper.findForSearch(FsUserBean);
    }


    /**
     * 查找记录返回listmap
     *
     * @param FsUserBean 查询条件
     * @return List<Map<String,Object>>
     */
    public List<Map<String,Object>> findForSearchAllUser(FsUserBean FsUserBean)
    {
        return mapper.findForSearchAllUser(FsUserBean);
    }

    /**
     * 导出excel
     * @param searchParam
     * @return
     */
    public List<Map<String, Object>> getListOfExcel(FsUserBean searchParam) {
        return mapper.getListOfExcel(searchParam);
    }

    /**
     * 查询单个用户
     * @param arg0
     * @return
     */
    public FsUserBean getSingleByParam(Integer arg0)
    {
        return mapper.getSingleByParam(arg0);
    }
    
}
